package com.cdck.safe.dete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdck.safe.dete.model.entity.Laboratory;
import com.cdck.safe.dete.mapper.LaboratoryMapper;
import com.cdck.safe.dete.model.entity.Seat;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.model.query.BaseQuery;
import com.cdck.safe.dete.service.LaboratoryService;
import com.cdck.safe.dete.service.SeatService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Laboratory)表服务实现类
 *
 * @author wp
 * @since 2024-02-25 15:40:15
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class LaboratoryServiceImpl extends ServiceImpl<LaboratoryMapper, Laboratory> implements LaboratoryService {

    private   final SeatService seatService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Laboratory getLaboratoryFormData(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * queryParams 筛选条件
     * @return 查询结果
     */
    @Override
    public IPage<Laboratory> queryByPage(BaseKeywordsQuery queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        Page<Laboratory> page = new Page<>(pageNum, pageSize);

        this.page(page, new LambdaQueryWrapper<Laboratory>()
                .like(StrUtil.isNotBlank(keywords), Laboratory::getName, keywords)
        );
        page.getRecords().stream().forEach(item -> {
            item.setCapacity((int) seatService.count(new LambdaQueryWrapper<Seat>().eq(Seat::getLabId, item.getId())));
        });

        return page;
     }

    /**
     * 新增数据
     *
     * @param laboratory 实例对象
     * @return 实例对象
     */
    @Override
    public boolean saveLaboratory(Laboratory laboratory) {
        this.save(laboratory);
        List<Seat> seatList = new ArrayList<>();
        // 获取实验室座位容量
        Integer capacity = laboratory.getCapacity();
        for (int i = 0; i < capacity; i++) {
            Seat  seat =   new   Seat();
            seat.setLabId(laboratory.getId());
            seat.setSeatNo(("s0"+(i+1)));
            seat.setStatus(0);
            seatList.add(seat);
        }
        seatService.saveBatch(seatList);
       return  true;
    }

    /**
     * 修改数据
     *
     * @param laboratory 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateLaboratory(Long id, Laboratory laboratory) {
         return this.updateById(laboratory);
    }

    /**
     * 通过主键删除数据
     *
     * @param idsStr ids集合
     * @return 是否成功
     */
    @Override
    public boolean deleteLaboratorys(String idsStr) {
         Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }
}
